<?php
class autoloader {
	
    protected $autoload_paths;
    protected $autoload_status;    
	
	public function __construct() {

        spl_autoload_register(array($this, 'autoload'));

        $this->autoload_paths = array();

        $this->autoload_status = false;

	}
	
    public function get_status() {
        return $this->autoload_status;
    }

    public function set_status($status){
        $this->autoload_status = $status;
        return $this;
    }

    public function add_path($path) {
        if(!is_dir($path)) {
            return false;
        }

        if(!in_array($path, $this->autoload_paths)){
             array_push($this->autoload_paths, $path);
        }

        return $this;
     }

    public function add_path_array($array){
        foreach($array as $path){
            $this->add_path($path);
        }
    }

    public function autoload($name) {
        if($this->autoload_status){ 
            $name = strtolower($name);
            foreach($this->autoload_paths as $path){
                if(file_exists($path . $name . '.php')){
                    include_once($path . $name . '.php');
                    return true;
                }
            } 
            throw new Exception('Could not auto-load file '. $name . '.php');
        }
    }
}
